<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="fmt" uri="http://www.springframework.org/tags" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>合同管理</title>
    <link rel="stylesheet" href="../../lib/layui/layui/css/layui.css">
    <script type="text/javascript" src="../../js/news/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="../../lib/layui/layui/lay/dest/layui.all.js"></script>
    <script src="../../js/base/base.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../lib/layui/layui/layui.js"></script>
    <style>
        * {font-family: "Microsoft Yahei" !important}
        nav{
            height: 57px;
            margin-bottom: -10px;
        }
        .search{
            display: inline;
            margin-bottom: 1px;
            width: 185px;
            border: 0;
            background: #fafbfc;
            height: 35px;
            margin-top: 5px;
        }
        .allBtn{
            float: left !important;
            margin-top: 11px;
            width: 65px;
            margin-left: -8px;
            border: none;
            height: 30px;
            line-height: 30px;
        }
        .layui-tab{
            margin: 0px;
        }
        nav .layui-form-select .layui-input{
            height: 35px;
        }
    </style>
</head>
<body>
<nav>
    <form class="layui-form" action="">
        <div class="layui-form-item" style="display: inline-block; margin-top: 10px;float: left">
            <div class="layui-inline">
                <label class="layui-form-label">合同标题</label>
                <div class="layui-input-inline">
                    <input type="text" name="title" lay-verify="required|phone" autocomplete="off" class="layui-input title">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">合同类型</label>
                <div class="layui-input-inline">
                    <select name="modules" id="dictName" lay-verify="required" lay-search="">
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">合同种类</label>
                <div class="layui-input-inline">
                    <select name="contractType" class="codeName" lay-verify="required" lay-search="">
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">合同金额</label>
                <div class="layui-input-inline" style="width: 140px">
                    <input type="text" name="title" lay-verify="required|phone" autocomplete="off" class="layui-input money">
                </div>
            </div>
            <span style="margin-left: -19px">~</span>
            <div class="layui-inline">
                <div class="layui-input-inline" style="width: 140px">
                    <input type="text" name="title" lay-verify="required|phone" autocomplete="off" class="layui-input moneyEnd">
                </div>
            </div>
        </div>
        <button type="button" class="layui-btn layui-btn-normal layui-btn-sm allBtn classBtn">查询</button>
        <%--新建合同--%>
        <div style="float: right; margin-top: 10px;">
            <button type="button" class="layui-btn layui-btn-normal layui-btn-sm export" style="margin-right: 12px;float: right !important;">导出</button>
            <button type="button" class="layui-btn layui-btn-primary layui-btn-sm import" style="float: right !important;margin-right: 15px;">导入</button>
        </div>
    </form>
</nav>
<div>
    <div class="layui-tab layui-tab-brief" lay-filter="docDemoTabBrief">
        <div class="layui-tab-content">
            <div class="content"><table class="layui-hide" id="plan" lay-filter="plan"></table></div>
        </div>
    </div>
</div>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="editUser">修改跟进人</a>
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="examine">查看</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
</body>
<script>
    $('.content').height($(window).height() - 130);
    $('.classBtn').click(function () {
        $(this).toggleClass("one")
        $(this).siblings('button').removeClass("one");
    });
    var exportData;
    layui.use(['form', 'layedit', 'laydate','layedit','table','upload'], function(){
        var form= layui.form
            ,layer = layui.layer
            ,laydate = layui.laydate
            ,layedit = layui.layedit
            ,table = layui.table
            ,upload = layui.upload;

        layedit.build('texPart'); //建立编辑器

        //合同种类
        $.ajax({
            url: '/code/getCode',
            type: 'get',
            dataType: 'json',
            data:{
                parentNo:'CONTRACT_TYPE'
            },
            success: function (res) {
                var obj = res.obj
                var str = ''
                for (var i = 0; i < obj.length; i++) {
                    str += '<option value="' + obj[i].codeNo + '">' + obj[i].codeName + '</option>'
                }
                $('.codeName').append(str);
                form.render('select');
            }
        })

        //合同类型
        $.ajax({
            url: '/contract/selectTypeAll',
            type: 'get',
            dataType: 'json',
            success: function (res) {
                var obj = res.data
                var str = ''
                for (var i = 0; i < obj.length; i++) {
                    str += '<option value="' + obj[i].typeId + '">' + obj[i].typeName + '</option>'
                }
                $('#dictName').append(str);
                form.render('select');
            }
        })

        $('.classBtn').click(function () {
            var money = ''
            var moneyEnd = ''
            var typeId = $('#dictName').val()
            var contractType = $('.codeName').val()
            if($('.money').val()==''&&$('.moneyEnd').val()!='') {
                money = '0'
                moneyEnd = $('.moneyEnd').val()
            }else if($('.money').val()!=''&&$('.moneyEnd').val()==''){
                money = $('.money').val()
                moneyEnd = '0'
            }else{
                money = $('.money').val()
                moneyEnd =$('.moneyEnd').val()
            }
            table.reload('plan',{
                where:{
                    typeId:typeId,
                    contractType:contractType,
                    money:money,
                    moneyEnd:moneyEnd,
                    title:$('.title').val()
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            })
        })
        $('.import').click(function () {
            layer.open({
                type: 1,
                title: ['数据导入'],
                area: ['750px', '400px'],
                shadeClose: true, //点击遮罩关闭
                content: '<form id="ajaxform" method="post" action="/contract/imports"  enctype="multipart/form-data"><div style="text-align: center">' +
                    '<button type="button" class="layui-btn layui-btn-normal" attachmentId="" attachmentName="" id="test3" style="height: 50px;width: 190px;margin-top: 70px"><i class="layui-icon"></i>选择文件</button>' +
                    '<div style="margin-top: 20px;font-size: 20px;"><a style="color: #03a9f4"  href="/file/contract/局重大合同台账.xls">导出模板</a></div>' +
                    '<hr style="height: 2px;margin-top: 100px;"/>' +
                    '<div><span style="color: #03a9f4;margin-right: 410px;">特别提示：在执行导入数据前,请先备份数据库</span></div></div></from>',
                success: function () {
                    upload.render({
                        elem: '#test3'
                        ,url: '/contract/imports'
                        ,accept: 'file' //普通文件
                        ,before:function(obj){
                            layer.load(); //上传loading
                        }
                        ,done: function(res){//返回值接收
                            layer.closeAll('loading'); //关闭loading
                            if(res.flag){
                                table.reload("plan", {});
                                layer.closeAll();
                            }
                            layer.msg(res.msg);
                        }
                        ,error: function(index, upload){
                            layer.closeAll('loading'); //关闭loading
                        }
                    });
                },
            });
        })
        form.render();
        //日期
        laydate.render({
            elem: '#date'
        });
        var ins1=table.render({
            elem: '#plan'
            ,url:'/contract/selectContractAll'
            ,title: '合同管理'
            ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            ,where:{
                useFlag:true
            }
            ,cols: [[
                {field:'contractNo',  title: '合同编号'}
                ,{field:'title', title: '合同标题'}
                ,{field:'typeName',  title: '合同类型'}
                ,{field:'targetName', title: '合同对象'}
                ,{field:'money', title: '合同金额'}
                ,{field:'userName', title: '跟进人'}
                ,{title: '操作', width:'265',toolbar: '#barDemo'}
            ]]
            ,parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": 0, //解析接口状态
                    "count": res.count, //解析数据长度
                    "data": res.data //解析数据列表
                };
            },
            page:true,
            done: function (res, curr, count) {
                exportData = res.data;
            }
        });
        //监听头工具栏事件
        table.on('tool(plan)', function(obj){
            switch(obj.event){
                case 'edit':
                    if(obj.length === 0){
                        layer.msg('请选择一行');
                    } else if(obj.length > 1){
                        layer.msg('只能同时编辑一个');
                    } else {
                        location.href='/contract/edit?contractId='+obj.data.contractId;
                    }
                    break;
                case 'del':
                    if(obj.length === 0){
                        layer.msg('请选择一行');
                    } else {
                        var ids=[];
                        ids.push(obj.data.contractId);
                        layer.confirm('确定要删除吗？', {
                            btn: ['确定','取消'], //按钮
                        }, function(){
                            $.ajax({
                                url: '/contract/delContract',
                                type: 'post',
                                dataType: 'json',
                                data:{
                                    ids:ids
                                },
                                success: function (data) {
                                    layer.msg('删除成功！')
                                    layer.close();
                                    table.reload("plan", {});
                                }
                            })
                        })

                    }
                    break;
                case 'examine':
                    location.href='/contract/details?contractId='+obj.data.contractId;
                    break;
                case 'editUser':
                    if(obj.length === 0){
                        layer.msg('请选择一行');
                    } else {
                        $.ajax({
                            url: '/contract/selectContractById',
                            type: 'get',
                            dataType: 'json',
                            data:{
                                contractId:obj.data.contractId
                            },
                            success: function (data) {
                                layer.open({
                                    type: 1,
                                    title: ['修改跟进人'],
                                    area: ['650px', '350px'],
                                    content:'<form style="text-align: center;padding-top: 40px;">' +
                                        '<div class="layui-form-item">' +
                                        '<div class="layui-block">' +
                                        '<label class="layui-form-label">合同编号</label>' +
                                        '<div class="layui-input-block">' +
                                        '<input type="text" style="width: 90%;border: none;" readonly name="contractNo" value="'+data.object.contractNo+'" autocomplete="off" class="layui-input">' +
                                        '</div>' +
                                        '</div>' +
                                        '<div class="layui-block" style="margin-top:25px">' +
                                        '<label class="layui-form-label">合同标题</label>' +
                                        '<div class="layui-input-block">' +
                                        '<input type="text" style="width: 90%;border: none;" name="title" value="'+data.object.title+'" autocomplete="off" class="layui-input">' +
                                        '</div>' +
                                        '</div>' +
                                        '<div class="layui-block" style="margin-top:25px">' +
                                        '<label class="layui-form-label">合同跟进人</label>' +
                                        '<div class="layui-input-block">' +
                                        '<input class="spanText" user_id="'+data.object.userId+'" style="margin-top: 7px" value="'+data.object.userName+'" id="governor"/>' +
                                        '<span class="add-item" style="margin-left: 10px"><i class="layui-icon layui-icon-add-circle"></i></span>' +
                                        '</div>' +
                                        '</div></div></form>',
                                    btn: ['保存'],
                                    yes:function(index){
                                        var str=$('.spanText').attr('user_id');
                                        var userId=str.substr(0,str.length-1);
                                        $.ajax({
                                            url: '/contract/upContractUserId',
                                            type: 'get',
                                            dataType: 'json',
                                            data:{
                                                contractId:obj.data.contractId,
                                                userId:userId
                                            },
                                            success: function (data) {
                                                var strname=$('.spanText').val();
                                                var userName=strname.substr(0,strname.length-1);
                                                layer.close(index)
                                                obj.update({
                                                    userName: userName
                                                });
                                            }
                                        })
                                    },
                                    success:function () {
                                        $('.layui-icon-add-circle').click(function () {
                                            user_id = 'governor';
                                            $.popWindow("/common/selectUser?0");
                                        });

                                    }
                                })
                            }
                        })
                    }
                    break;
            };
        });

        //导出按钮
        $('.export').click(function () {
            var money = ''
            var moneyEnd = ''
            var typeId = $('#dictName').val()
            var contractType = $('.codeName').val()
            if($('.money').val()==''&&$('.moneyEnd').val()!='') {
                money = '0'
                moneyEnd = $('.moneyEnd').val()
            }else if($('.money').val()!=''&&$('.moneyEnd').val()==''){
                money = $('.money').val()
                moneyEnd = '0'
            }else{
                money = $('.money').val()
                moneyEnd =$('.moneyEnd').val()
            }
           window.location.href='/contract/selectContractAll?isExport='+true+'&contractType='+contractType+'&typeId='+typeId+'&money='+money+'&moneyEnd='+moneyEnd+'&title='+$('.title').val()
        })

    });

</script>

</html>
